<template>
  <div class="certification h-[100%] flex py-10 px-[20rem]">
    <el-steps :active="stepActive" direction="vertical" finish-status="success" class="steps-wrap">
      <el-step :title="step.title" v-for="(step, idx) of stepList" :key="idx" />
    </el-steps>

    <div class="step-content flex-1">
      <div class="scroll">
        <div v-show="stepActive === 0 || stepActive === 3">
          <BaseForm ref="baseFormRef" />
        </div>
        <div v-show="stepActive === 1 || stepActive === 3">
          <IDDocumentForm ref="idDocumentFormRef" />
        </div>
        <div v-show="stepActive === 2 || stepActive === 3">
          <el-row>
            <el-col>
              <el-result
                  icon="success"
                  title="提交成功"
                  sub-title="您提交的信息已经收到，我们将在1-3个工作日内完成审核，请耐心等待。"
              >
                <template #extra>
                  <el-button type="primary" @click="router.push('/login')">返回登录界面</el-button>
                </template>
              </el-result>
            </el-col>
          </el-row>
        </div>
      </div>
      <div class="btn_wrap">
        <div>
          <el-button
              v-show="stepActive > 0 && stepActive < 2"
              type="primary"
              @click="handlePrev"
          >上一步</el-button>
        </div>
        <div>
          <el-button
              v-show="stepActive < 2"
              type="primary"
              @click="handleNext"
          >下一步
          </el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import {reactive, ref} from 'vue'
import BaseForm from "~/views/RegisterView/components/BaseForm.vue";
import IDDocumentForm from "~/views/RegisterView/components/IDDocumentForm.vue";
import AuditForm from "~/views/RegisterView/components/AuditForm.vue";
import { useRouter } from "vue-router";
const router = useRouter();
// 步骤信息
const stepList = reactive([{
  title: '创建账号'
}, {
  title: '注册申请'
}, {
  title: '信息审核'
}, {
  title: '注册完成'
}]);
const stepActive = ref(0)

let baseFormRef = ref<BaseForm>(); // 基本信息
let idDocumentFormRef = ref<IDDocumentForm>(); // 证件信息
let auditFormRef = ref<AuditForm>(); // 审核信息

const handlePrev = () => {
  if (stepActive.value-- < 0) stepActive.value = 0
}
const handleNext = async () => {
  switch (stepActive.value) {
    case 0: // 基本信息已填完
      try {
        await passBaseForm();
      } catch (err) {
        return false;
      }
      break;
    case 1: // 场景已配置
      try {
        await passIDDocumentForm();
      } catch (err) {
        return false;
      }
      break;
    case 2: // 调整内容已调整
      try {
        await passAuditForm();
      } catch (err) {
        return false;
      }
      break;
    default:
      break;
  }
  if (stepActive.value < 4) {
    stepActive.value += 1;
    if (stepActive.value > 3) {
      router.push('/');
    }
  }
}

const passBaseForm = async () => {
  return new Promise((resolve, reject) => {
    baseFormRef.value.$refs.ruleFormRef.validate(async (valid, fields) => {
      if (valid) {
        const params = baseFormRef.value.ruleForm;
        console.log(params);
        resolve(true);
      } else {
        console.log('error submit!', fields);
        reject(false);
      }
    });
  });
}

const passIDDocumentForm = async () => {
  return new Promise((resolve, reject) => {
    idDocumentFormRef.value.$refs.ruleFormRef.validate(async (valid, fields) => {
      if (valid) {
        const params = idDocumentFormRef.value.ruleForm;
        console.log(params);
        resolve(true);
      } else {
        console.log('error submit!', fields);
        reject(false);
      }
    });
  });
}

const passAuditForm = async () => {
  return new Promise((resolve, reject) => {
    auditFormRef.value.$refs.ruleFormRef.validate(async (valid, fields) => {
      if (valid) {
        const params = auditFormRef.value.ruleForm;
        console.log(params);
        resolve(true);
      } else {
        console.log('error submit!', fields);
        reject(false);
      }
    });
  });
}
</script>

<style lang="scss" scoped>
.steps-wrap {
  width: 170px;
  height: 300px;
}

::v-deep(.steps-wrap) {
  .el-step__line {
    position: absolute;
    top: 30px;
    left: 12px;
    width: 1px;
    bottom: 5px;
    display: block;
  }
  .el-step__head.is-process .el-step__icon {
    background-color: var(--el-color-primary);
    color: var(--el-bg-color);
    border-color: var(--el-color-primary);
  }
}

.step-content {
  height: 100%;
  position: relative;
  padding-bottom: 50px;
  box-sizing: border-box;

  .scroll {
    height: 100%;
    overflow: scroll;
  }
  .btn_wrap {
    display: flex;
    justify-content: space-between;
    // position: absolute;
    // bottom: 0;
    // left: 50%;
    // transform: translateX(-50%);
    // padding-right: 153px;
    // display: flex;
  }
}
</style>
